#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>

int main()
{
    int n;
    cin >> n;
    vector<vector<int>> arr(n, vector<int>(n, 0));
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            cin >> arr[i][j];
    int q;
    cin >> q;
    int row = 0, col = 0;
    while (q--)
    {
        int x;
        cin >> x;
        //cout<<x<<endl;
        if (x == 1)
        {
            row++;
            col++;
        }
        else
        {
            row++;
        }
    }
    //cout<<"_____"<<endl;
    //cout<<row<<":"<<col<<endl;
    if (row % 2)
    {
        for (int i = 0; i < n / 2; i++)
        {
            for (int j = 0; j < n; j++)
            {
                int tmp = arr[i][j];
                arr[i][j] = arr[n - i - 1][j];
                arr[n - i - 1][j] = tmp;
            }

        }
    }
    //     for(int i = 0;i<n;i++)
    //     {
    //         for(int j = 0;j<n;j++)
    //         {
    //             printf("%4d ",arr[i][j]);
    //         }
    //         cout<<endl;
    //     }
    if (col % 2)
    {
        for (int i = 0; i < n / 2; i++)
        {
            for (int j = 0; j < n; j++)
            {
                int tmp = arr[j][i];
                arr[j][i] = arr[j][n - i - 1];
                arr[j][n - i - 1] = tmp;
            }

        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            printf("%d ", arr[i][j]);
        }
        cout << endl;
    }
    return 0;
}

#include <iostream>
using namespace std;
#include <vector>

int main() {
    int n, k;
    cin >> n >> k;
    vector<long long> a(n + 1, 0);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    vector<vector<long long>> dp(n + 1, vector<long long>(k, -0x3f3f3f3f));
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j < k; j++)
        {
            dp[i][j] = dp[i - 1][j];
            if (dp[i - 1][(j - a[i] % k + k) % k] != -0x3f3f3f3f)
            {
                dp[i][j] = max(dp[i][j], dp[i - 1][(j - a[i] % k + k) % k] + a[i]);
            }
        }
    }
    if (dp[n][0] <= 0)
        cout << -1 << endl;
    else
        cout << dp[n][0] << endl;
    return 0;
}